import { useInitDataSingleton } from './shared';
import { BasShuXueZhiZhengApi, BasShuXueZhiZhengOutput } from '/@/api/bas/BasShuXueZhiZheng';

const {
	list,
	initList: getBasShuXueZhiZhengList, initListForce: initShuXueZhiZhengForce } = useInitDataSingleton<BasShuXueZhiZhengOutput>(BasShuXueZhiZhengApi.all);

// 输血指征, 不常用资料, 按需加载
export { getBasShuXueZhiZhengList, initShuXueZhiZhengForce };

// 通过id获取
export async function getBasShuXueZhiZheng(id: number) {
	await getBasShuXueZhiZhengList();
	return list.value.find((item) => item.id == id);
}

// 获取树形结构数据
export async function getBasShuXueZhiZhengTree() {
	await getBasShuXueZhiZhengList();
	// pId为0则为父节点,pId为-1则为没有子节点的一级节点
	let fatherList = list.value.filter((e) => e.pId == 0 || e.pId == -1);
	fatherList = fatherList.map((father) => {
		let children = list.value.filter((e) => e.pId == father.id);
		if(children.length > 0) {
			//@ts-ignore
			father.children = children;
		}
		return father;
	});
	return fatherList;
}

